package com.liit.controller.weixin.process;

import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;

import cn.liit.weixin.core.basic.accesstoken.AccessTokenSupport;
import cn.liit.weixin.core.config.Config;
import cn.liit.weixin.message.model.req.ReqBaseMessage;
import cn.liit.weixin.message.model.req.ScanEventMessage;
import cn.liit.weixin.message.model.resp.RespBaseMessage;
import cn.liit.weixin.message.model.resp.TextMessage;
import cn.liit.weixin.message.process.receive.ReceiveProcessor;
import cn.liit.weixin.userinfo.model.UserInfo;
import cn.liit.weixin.userinfo.utils.Util;

import com.liit.entity.MeetingFormMap;
import com.liit.entity.MeetingWxuserFormMap;
import com.liit.entity.WeixinUserFormMap;
import com.liit.mapper.WeixinUserMapper;
import com.liit.spring.SpringUtils;

/**
 * 
 * @author dingweibo
 * @Title: ScanMsgProcess.java
 * @Description: 扫码事件
 * @date 2016年2月15日
 */
public class ScanMsgProcess extends ReceiveProcessor {

	public ScanMsgProcess(String msgType, String event) {
		super(msgType, event);
	}

	@Override
	public RespBaseMessage doProcess(ReqBaseMessage reqBaseMessage) {
		RespBaseMessage respBaseMessage = null;
		try {
			String openid = reqBaseMessage.getFromUserName();
			UserInfo userInfo = Util.getUserInfo(AccessTokenSupport.getAccess_token(), openid);

			WeixinUserMapper mapper = SpringUtils.getBean("weixinUserMapper");
			WeixinUserFormMap weixinUserFormMap = mapper.findbyFrist("openid", openid, WeixinUserFormMap.class);
			if (weixinUserFormMap == null) {
				throw new NullPointerException("openid not found");
			}
			weixinUserFormMap.put("name", userInfo.getNickname());
			weixinUserFormMap.put("headerUrl", userInfo.getHeadimgurl());
			mapper.editEntity(weixinUserFormMap);

			// 已关注 带参数扫码
			ScanEventMessage req = (ScanEventMessage) reqBaseMessage;
			if (StringUtils.isBlank(req.getEventKey())) {
				throw new NullPointerException("eventKey is null!");
			}
			String meetingId = req.getEventKey();
			if (weixinUserFormMap.getInt("isReg") == 0) {
				if (StringUtils.isNotEmpty(req.getEventKey())) {
					respBaseMessage = new TextMessage("<a href='" + Config.notifyurl + "/weixin/reg.shtml?openid=" + openid + "'>您还没有注册，请点击注册</a>");
				}
			} else {
				MeetingFormMap meetingFormMap = mapper.findbyFrist("id", meetingId, MeetingFormMap.class);
				// 会议关闭
				if (meetingFormMap.getInt("status") == 1) {
					respBaseMessage = new TextMessage("您好!会议【" + meetingFormMap.getStr("name") + "】已关闭");
				} else {
					MeetingWxuserFormMap meetingWxuserFormMap = new MeetingWxuserFormMap();
					meetingWxuserFormMap.put("openid", openid);
					List<MeetingWxuserFormMap> list = mapper.findByNames(meetingWxuserFormMap);
					if (CollectionUtils.isEmpty(list)) {
						MeetingWxuserFormMap record = new MeetingWxuserFormMap();
						record.put("meetingId", meetingId);
						record.put("openid", openid);
						mapper.addEntity(record);
						respBaseMessage = new TextMessage("您好!欢迎加入【" + meetingFormMap.getStr("name") + "】");
					} else {
						if (list.get(0).get("meetingId").toString().equals(meetingId)) {
							respBaseMessage = new TextMessage("您好!您已加入【" + meetingFormMap.getStr("name") + "】");
						} else {
							respBaseMessage = new TextMessage("您好!您已加入其它会议,请先退出会议");
						}
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

		return respBaseMessage;
	}
}
